package com.songkun.template.utils;

import android.os.Environment;
import android.util.Log;

import com.songkun.template.BuildConfig;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ILog {
    private final static int NO_LOG = -1;
    private final static int LOG_V = Log.VERBOSE;
    private final static int LOG_D = Log.DEBUG;
    private final static int LOG_I = Log.INFO;
    private final static int LOG_W = Log.WARN;
    private final static int LOG_E = Log.ERROR;

    private static int logLevel = LOG_E;

    /**
     * 是否保存至SD卡
     */
    private static final boolean SAVE_TO_SD = BuildConfig.DEBUG ? false : true;

    /**
     * 日志打印时间Format
     */
    private static final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat LOG_FMT = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 保存LOG日志的目录
     */
    private static String LogDirPath;

    //对签名apk自动屏蔽日志
    static {
        logLevel = BuildConfig.DEBUG ? LOG_E : NO_LOG;
    }

    public static void v(String msg) {
        if (logLevel >= LOG_V) {
            Log.v(generateTAG(msg), "----" + msg + "----");
            if (SAVE_TO_SD) {
                storeLog(generateTAG(msg), msg);
            }
        }
    }

    public static void d(String msg) {
        if (logLevel >= LOG_D) {
            Log.d(generateTAG(msg),  "----" + msg + "----");
            if (SAVE_TO_SD) {
                storeLog(generateTAG(msg), msg);
            }
        }
    }

    public static void i(String msg) {
        if (logLevel >= LOG_I) {
            Log.i(generateTAG(msg),  "----" + msg + "----");
            if (SAVE_TO_SD) {
                storeLog(generateTAG(msg), msg);
            }
        }
    }

    public static void w(String msg) {
        if (logLevel >= LOG_W) {
            Log.w(generateTAG(msg),  "----" + msg + "----");
            if (SAVE_TO_SD) {
                storeLog(generateTAG(msg), msg);
            }
        }
    }

    public static void e(String msg) {
        if (logLevel >= LOG_E) {
            Log.e(generateTAG(msg),  "----" + msg + "----");
            if (SAVE_TO_SD) {
                storeLog(generateTAG(msg), msg);
            }
        }
    }

    private static String generateTAG(String msg) {
        StackTraceElement stackTrace = Thread.currentThread().getStackTrace()[4];
        String fullClassName = stackTrace.getClassName();
        if (null == msg) {
            int lineNumber = stackTrace.getLineNumber();
            throw new IllegalArgumentException(fullClassName + "第 " + lineNumber + " 行方法参数不能为null！");
        }
        return fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
    }

    /**
     * 将日志信息保存至SD卡
     * @param strModule LOG TAG
     * @param strErrMsg 保存的打印信息
     */
    public static void storeLog(String strModule, String strErrMsg) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            synchronized (LOG_FMT) {
                String logname = LOG_FMT.format(new Date(System.currentTimeMillis())) + ".txt";

                File file = new File(getLogDirPath() + "/" + logname);
                // 判断日志文件是否已经存在
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    // 输出
                    FileOutputStream fos = new FileOutputStream(file, true);
                    PrintWriter out = new PrintWriter(fos);

                    out.println(fmt.format(System.currentTimeMillis()) + "  >>" + strModule + "<<  " + strErrMsg + '\r');
                    out.flush();
                    out.close();

                }
                catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取Log保存目录
     * @return
     */
    public static String getLogDirPath() {
        return LogDirPath;
    }

    /**
     * 设置Log保存目录
     * @param dir
     */
    public static void setLogDirPath(String dir) {
        LogDirPath = dir;
    }
}
